import { isIdentityId } from './isIdentityId'
import { defineComponent } from 'vue'
const toString = Object.prototype.toString
/**
 * 判断值得类型
 * @param {*} val
 * @param {*} type
 * @returns
 */
export const is = (val, type) => {
	return toString.call(val) === `[object ${type}]`
}
/**
 * 判断val 是否是数字
 */
export const isNumber = val => {
	return is(val, 'Number')
}

/**
 * 判断url是否是http或https
 * @param {string} path
 * @returns {Boolean}
 */
export function isHttp(url) {
	return url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1
}

/**
 * 判断path是否为外链
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
	return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
	const valid_map = ['admin', 'editor']
	return valid_map.indexOf(str.trim()) >= 0
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
export function validURL(url) {
	const reg =
		/^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
	return reg.test(url)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str) {
	const reg = /^[a-z]+$/
	return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str) {
	const reg = /^[A-Z]+$/
	return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str) {
	const reg = /^[A-Za-z]+$/
	return reg.test(str)
}

/**
 * @param {string} email
 * @returns {Boolean}
 */
export function validEmail(email) {
	const reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
	return reg.test(email)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(str) {
	if (typeof str === 'string' || str instanceof String) {
		return true
	}
	return false
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg) {
	if (typeof Array.isArray === 'undefined') {
		return Object.prototype.toString.call(arg) === '[object Array]'
	}
	return Array.isArray(arg)
}

/* 是否手机号码或者固话*/
//@ts-ignore
export function validatePhoneTwo(rule, value, callback) {
	const reg =
		/((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/
	if (value == '' || value == undefined || value == null) {
		callback(new Error('请输入电话号码或者固话号码'))
	} else {
		if (!reg.test(value) && value != '') {
			callback(new Error('请输入正确的电话号码或者固话号码'))
		} else {
			callback()
		}
	}
}

/* 是否手机号码或者固话*/
//@ts-ignore
export function validatePhoneNegative(rule, value, callback) {
	const reg =
		/((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/
	if (value == '' || value == undefined || value == null) {
		callback()
	} else {
		if (!reg.test(value) && value != '') {
			callback(new Error('请输入正确的电话号码或者固话号码'))
		} else {
			callback()
		}
	}
}

/* 是否固话*/
//@ts-ignore
export function validateTelphone(rule, value, callback) {
	const reg = /^((0\d{2,3})-)?(\d{7,8})(-(\d{3,4}))?$/
	if (value == '' || value == undefined || value == null) {
		callback()
	} else {
		if (!reg.test(value) && value != '') {
			callback(new Error('请输入正确的固话（格式：区号+号码,如010-1234567）'))
		} else {
			callback()
		}
	}
}

/* 是否手机号码*/
//@ts-ignore
export function validatePhone(rule, value, callback) {
	const reg =
		// /^((\+?86)|(\(\+86\)))?((13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|17[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9]|19[0-9])\d{8})$/
		/^((\+?86)|(\(\+86\)))?((1[0-9])\d{9})$/
	if (value == '' || value == undefined || value == null) {
		return Promise.reject('请输入正确的电话号码')
	} else {
		if (!reg.test(value) && value != '') {
			return Promise.reject('请输入正确的电话号码')
		} else {
			return Promise.resolve()
		}
	}
}

/* 是否手机号码*/
//@ts-ignore
export function validatePhoneNot(rule, value, callback) {
	const reg =
		// /^((\+?86)|(\(\+86\)))?((13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|17[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9]|19[0-9])\d{8})$/
		/^((\+?86)|(\(\+86\)))?((1[0-9])\d{9})$/
	if (value == '' || value == undefined || value == null) {
		callback()
	} else {
		if (!reg.test(value) && value != '') {
			return Promise.reject('请输入正确的电话号码')
		} else {
			return Promise.resolve()
		}
	}
}

/* 是否身份证号码*/
//@ts-ignore
export function validateIdNo(rule, value, callback) {
	if (value == '' || value == undefined || value == null) {
		callback(new Error('身份证号不能为空'))
	} else {
		if (isIdentityId(value) !== '') {
			callback(new Error(isIdentityId(value)))
		} else {
			callback()
		}
	}
}

/* 是否邮箱*/
//@ts-ignore
export function validateEMail(rule, value, callback) {
	const reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
	if (value == '' || value == undefined || value == null) {
		callback()
	} else {
		if (!reg.test(value)) {
			callback(new Error('请输入正确的邮箱地址'))
		} else {
			callback()
		}
	}
}

/* 是否邮编*/
//@ts-ignore
export function validatePostcode(rule, value, callback) {
	const reg = /[1-9]\d{5}(?!\d)/
	if (value == '' || value == undefined || value == null) {
		callback()
	} else {
		if (!reg.test(value)) {
			callback(new Error('请输入正确的邮编'))
		} else {
			callback()
		}
	}
}

/* 是否金额*/
//@ts-ignore
export function isMoney(rule, value, callback) {
	const reg = /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/
	if (value == '' || value == undefined || value == null) {
		callback(new Error('请输入合法的金额数字，最多两位小数'))
	} else {
		if (!reg.test(value)) {
			callback(new Error('请输入合法的金额数字，最多两位小数'))
		} else {
			callback()
		}
	}
}

/* 是否金额非必填*/
//@ts-ignore
export function isMoneyNot(rule, value, callback) {
	const reg = /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/
	if (value == '' || value == undefined || value == null) {
		callback()
	} else {
		if (!reg.test(value)) {
			callback(new Error('请输入合法的金额数字，最多两位小数'))
		} else {
			callback()
		}
	}
}

/* 是否金额非必填*/
//@ts-ignore
export function isMoneyNegative(rule, value, callback) {
	const reg = /^(-([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/
	if (value == '' || value == undefined || value == null) {
		callback()
	} else {
		if (!reg.test(value)) {
			callback(new Error('请输入合法的金额数字，最多两位小数'))
		} else {
			callback()
		}
	}
}

/* 校验数字小数点后几位非必填*/
//@ts-ignore
// maxDecimals后几位
export function maxDecimals(rule, value, callback, maxDecimals = 2) {
	const reg = new RegExp(`^-?\\d+(\\.\\d{1,${maxDecimals}})?$`)
	if (value == '' || value == undefined || value == null) {
		callback()
	} else {
		if (!reg.test(value)) {
			callback(new Error(`请输入合法的数字，最多${maxDecimals}位小数`))
		} else {
			callback()
		}
	}
}

/* 是否数字*/
//@ts-ignore
export function isNum(rule, value, callback) {
	const reg = /^[0-9]*$/
	if (value == '' || value == undefined || value == null) {
		callback()
	} else {
		if (!reg.test(value)) {
			callback(new Error('输入的值只能为整数'))
		} else {
			callback()
		}
	}
}

// 验证最大长度
export function validateLen(len) {
	return {
		max: len,
		message: '内容最大长度为' + len + '个字符',
		trigger: 'blur'
	}
}

/* 18位长度*/
//@ts-ignore
export function length18(rule, value, callback) {
	const reg = /^\d{n}$/
	if (value == '' || value == undefined || value == null) {
		callback()
	} else {
		if (!reg.test(value)) {
			callback(new Error('请输入的18位数'))
		} else {
			callback()
		}
	}
}

/* 弱密码检查*/
//@ts-ignore
export function isMulPassword(rule, value, callback) {
	console.log('====》');
	const pattern = /^.*(?=.{6,20})(?=.*\d)(?=.*[A-Za-z]{1,})(?=.*[_@:.&!*+]).*$/
	if (value == '' || value == undefined || value == null) {
		callback()
	} else {
		if (!pattern.test(value)) {
			callback(new Error('请输入6-20位数字字母以及_@:.&!*+符号组合'))
		} else {
			callback()
		}
	}
}

export default defineComponent({
	setup() {
		return {
			validatePostcode,
			validateEMail,
			validateIdNo,
			validatePhone,
			validateTelphone,
			isMoney,
			isMoneyNot,
			isNum,
			length18,
			isMulPassword,
			validateLen,
			isExternal,
			isHttp
		}
	}
})